package com.yunfeisoft.base.dao.impl;

import com.yunfeisoft.base.dao.inter.WorkflowStepDao;
import com.yunfeisoft.base.model.WorkflowStep;
import com.yunfeisoft.common.base.BaseDaoImpl;
import com.yunfeisoft.common.sql.builder.WhereBuilder;
import com.yunfeisoft.common.utils.Page;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * ClassName: WorkflowStepDaoImpl
 * Description: 流程步骤定义Dao实现
 * Author: Jackie liu
 * Date: 2020-01-03
 */
@Repository
public class WorkflowStepDaoImpl extends BaseDaoImpl<WorkflowStep, String> implements WorkflowStepDao {

    @Override
    public Page<WorkflowStep> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            initPageParam(wb, params);
        }
        return queryPage(wb);
    }

    @Override
    public int removeByWorkflowDefineId(String workflowDefineId) {
        String sql = String.format("DELETE FROM %s WHERE WORKFLOW_DEFINE_ID_ = ?", domainModel.getTableName());
        return jdbcTemplate.update(sql, workflowDefineId);
    }

    @Override
    public List<WorkflowStep> queryByWorkflowDefineId(String workflowDefineId) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithAsc("stepNum");
        wb.andEquals("workflowDefineId", workflowDefineId);
        return query(wb);
    }

    @Override
    public WorkflowStep getNextStep(String workflowDefineId, int stepNum) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithAsc("stepNum");
        wb.andEquals("workflowDefineId", workflowDefineId);
        wb.andGreaterThan("stepNum", stepNum);

        wb.setPageNum(0);
        wb.setPageSize(1);
        List<WorkflowStep> stepList = queryLimit(wb);
        if (CollectionUtils.isEmpty(stepList)) {
            return null;
        }
        return stepList.get(0);
    }
}